package com.handydata.antifraud.web;

import java.util.List;
import java.util.stream.Collectors;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.handydata.antifraud.bo.datacalllogs.DataCallLogsReqBO;
import com.handydata.antifraud.bo.datacalllogs.DataCallLogsResBO;
import com.handydata.antifraud.bo.datacalllogs.QueryDataOriginManageDetailReqBO;
import com.handydata.antifraud.bo.datacalllogs.QueryDataOriginManageDetailResBO;
import com.handydata.antifraud.bo.datacalllogs.QueryDataOriginManageReqBO;
import com.handydata.antifraud.bo.datacalllogs.QueryDataOriginManageResBO;
import com.handydata.antifraud.common.exception.DataCallLogsExCode;
import com.handydata.antifraud.service.DataCallLogsService;
import com.handydata.antifraud.vo.datacalllogs.CreateDataCallLogsReqVO;
import com.handydata.antifraud.vo.datacalllogs.CreateDataCallLogsResVO;
import com.handydata.antifraud.vo.datacalllogs.DataCallLogsBeanVO;
import com.handydata.antifraud.vo.datacalllogs.DataOriginDetailBeanVO;
import com.handydata.antifraud.vo.datacalllogs.DataOriginManageBeanVO;
import com.handydata.antifraud.vo.datacalllogs.DeleteDataCallLogsReqVO;
import com.handydata.antifraud.vo.datacalllogs.DeleteDataCallLogsResVO;
import com.handydata.antifraud.vo.datacalllogs.ModifyDataCallLogsReqVO;
import com.handydata.antifraud.vo.datacalllogs.ModifyDataCallLogsResVO;
import com.handydata.antifraud.vo.datacalllogs.QueryDataCallLogsReqVO;
import com.handydata.antifraud.vo.datacalllogs.QueryDataCallLogsResVO;
import com.handydata.antifraud.vo.datacalllogs.QueryDataOriginManageBeanVO;
import com.handydata.antifraud.vo.datacalllogs.QueryDataOriginManageDetailReqVO;
import com.handydata.antifraud.vo.datacalllogs.QueryDataOriginManageDetailResVO;
import com.handydata.antifraud.vo.datacalllogs.QueryDataOriginManageReqVO;
import com.handydata.antifraud.vo.datacalllogs.QueryDataOriginManageResVO;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.web.AbsController;

@RequestMapping("/datacalllogs")
@RestController
public class DataCallLogsController extends AbsController {
	
	private static final Logger logger = LoggerFactory.getLogger(DataCallLogsController.class);
	
	@Autowired
	private DataCallLogsService dataCallLogsService;
	
	@ApiOperation(value="新增调用数据平台日志", notes="新增调用数据平台日志")
	@ApiImplicitParam(name = "req", value = "新增调用数据平台日志请求对象", required = true, dataType = "CreateDataCallLogsReqVO", paramType = "body")
	@PostMapping("/create")
	public CreateDataCallLogsResVO createDataCallLogs(@RequestBody CreateDataCallLogsReqVO req) {
		try {
			logger.info("Execute createDataCallLogs, param is {}", req);
			DataCallLogsReqBO reqBO = CommonBeanConvertor.convert(req, DataCallLogsReqBO.class);
			DataCallLogsResBO resBO = dataCallLogsService.createDataCallLogs(reqBO);
			CreateDataCallLogsResVO res = CommonBeanConvertor.convert(resBO, CreateDataCallLogsResVO.class);
			return res;
		} catch (Exception e) {
			return handleException(e, CreateDataCallLogsResVO.class, req, DataCallLogsExCode.SYS_UNKNOWN_ERROR);
		}
	}

	@ApiOperation(value="修改调用数据平台日志", notes="修改调用数据平台日志")
	@ApiImplicitParam(name = "req", value = "修改调用数据平台日志请求对象", required = true, dataType = "ModifyDataCallLogsReqVO", paramType = "body")
	@PostMapping("/modify")
	public ModifyDataCallLogsResVO modifyDataCallLogs(@RequestBody ModifyDataCallLogsReqVO req) {
		try {
			logger.info("Execute modifyDataCallLogs, param is {}", req);
			DataCallLogsReqBO reqBO = CommonBeanConvertor.convert(req, DataCallLogsReqBO.class);
			DataCallLogsResBO resBO = dataCallLogsService.modifyDataCallLogs(reqBO);
			ModifyDataCallLogsResVO res = CommonBeanConvertor.convert(resBO, ModifyDataCallLogsResVO.class);
			return res;
		} catch (Exception e) {
			return handleException(e, ModifyDataCallLogsResVO.class, req, DataCallLogsExCode.SYS_UNKNOWN_ERROR);
		}
	}

	@ApiOperation(value="删除调用数据平台日志", notes="删除调用数据平台日志")
	@ApiImplicitParam(name = "req", value = "修改调用数据平台日志请求对象", required = true, dataType = "DeleteDataCallLogsReqVO", paramType = "body")
	@PostMapping("/remove")
	public DeleteDataCallLogsResVO deleteDataCallLogs(@RequestBody DeleteDataCallLogsReqVO req) {
		try {
			logger.info("Execute deleteDataCallLogs, param is {}", req);
			DataCallLogsReqBO reqBO = CommonBeanConvertor.convert(req, DataCallLogsReqBO.class);
			DataCallLogsResBO resBO = dataCallLogsService.deleteDataCallLogs(reqBO);
			DeleteDataCallLogsResVO res = CommonBeanConvertor.convert(resBO, DeleteDataCallLogsResVO.class);
			return res;
		} catch (Exception e) {
			return handleException(e, DeleteDataCallLogsResVO.class, req, DataCallLogsExCode.SYS_UNKNOWN_ERROR);
		}
	}
	
	@ApiOperation(value="查询调用数据平台日志", notes="查询调用数据平台日志")
	@ApiImplicitParam(name = "req", value = "查询调用数据平台日志请求对象", required = true, dataType = "QueryDataCallLogsReqVO", paramType = "body")
	@PostMapping("/query")
	public QueryDataCallLogsResVO queryDataCallLogs(@RequestBody QueryDataCallLogsReqVO req) {
		try {
			logger.info("Execute queryDataCallLogs, param is {}", req);
			DataCallLogsReqBO reqBO = CommonBeanConvertor.convert(req, DataCallLogsReqBO.class);
			DataCallLogsResBO resBO = dataCallLogsService.queryDataCallLogs(reqBO);
			QueryDataCallLogsResVO res = CommonBeanConvertor.convert(resBO, QueryDataCallLogsResVO.class, DataCallLogsBeanVO.class);
			return res;
		} catch (Exception e) {
			return handleException(e, QueryDataCallLogsResVO.class, req, DataCallLogsExCode.SYS_UNKNOWN_ERROR);
		}
	}
	
	@ApiOperation(value="数据源管理查询", notes="数据源管理查询")
	@ApiImplicitParam(name = "req", value = "数据源管理查询请求对象", required = true, dataType = "QueryDataOriginManageReqVO", paramType = "body")
	@PostMapping("/dataOriginManageListQuery")
	public QueryDataOriginManageResVO queryDataOriginManageList(@RequestBody QueryDataOriginManageReqVO req) {
		QueryDataOriginManageResVO res;
		try {
			logger.info("Execute queryDataOriginManage, param is {}", req);
			QueryDataOriginManageReqBO reqBO = CommonBeanConvertor.convert(req, QueryDataOriginManageReqBO.class);
			QueryDataOriginManageResBO resBO = dataCallLogsService.queryDataOriginManage(reqBO);
			res = CommonBeanConvertor.convert(resBO, QueryDataOriginManageResVO.class, QueryDataOriginManageBeanVO.class);
			res.setRespCode("0000");
			res.setRespMsg("调用成功");
			return res;
		} catch (Exception e) {
			return handleException(e, QueryDataOriginManageResVO.class, req, DataCallLogsExCode.SYS_UNKNOWN_ERROR);
		}
	}
	
	@ApiOperation(value="数据源统计明细", notes="数据源统计明细")
	@ApiImplicitParam(name = "req", value = "数据源统计明细请求对象", required = true, dataType = "QueryDataOriginManageDetailReqVO", paramType = "body")
	@PostMapping("/dataOriginManageDetailQuery")
	public QueryDataOriginManageDetailResVO queryDataOriginManageDetail(@RequestBody QueryDataOriginManageDetailReqVO req) {
		QueryDataOriginManageDetailResVO res = new QueryDataOriginManageDetailResVO();
		try {
			logger.info("Execute queryDataOriginManageDetail, param is {}", req);
			QueryDataOriginManageDetailReqBO reqBO = CommonBeanConvertor.convert(req, QueryDataOriginManageDetailReqBO.class);
			QueryDataOriginManageDetailResBO resBO = dataCallLogsService.queryDataOriginManageDetail(reqBO);
			if (resBO.getDataOriginDetailInfo() != null) {
				DataOriginDetailBeanVO dataOriginDetailInfo = CommonBeanConvertor.convert(resBO.getDataOriginDetailInfo(), DataOriginDetailBeanVO.class);
				res.setDataOriginDetailInfo(dataOriginDetailInfo);
			}
			if (resBO.getChartList() != null && resBO.getChartList().size() > 0) {
				List<DataOriginManageBeanVO> chartList = resBO
						.getChartList()
						.stream()
						.map(x -> CommonBeanConvertor.convert(x,DataOriginManageBeanVO.class))
						.collect(Collectors.toList());
				res.setChartList(chartList);
			}
			res.setRespCode("0000");
			res.setRespMsg("调用成功");
			return res;
		} catch (Exception e) {
			return handleException(e, QueryDataOriginManageDetailResVO.class, req, DataCallLogsExCode.SYS_UNKNOWN_ERROR);
		}
	}
	
}
